package com.example.http_image;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

    public class MainActivity extends AppCompatActivity {

    private ImageView mImageView;
    private LoadImageTask mLoadImageTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        if (Build.VERSION.SDK_INT >= 23){
//            int REQUEST_CODE_CONTACT = 101;
//            String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
//
//            for (String str : permissions){
//                if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED){
//                    this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
//                    return;
//                }
//            }
//        }


        mImageView = findViewById(R.id.imageView);
        mLoadImageTask = new LoadImageTask("https://img3.mukewang.com/5333a2590001069f02000200-80-80.jpg");
        mLoadImageTask.execute();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mLoadImageTask != null){
            mLoadImageTask.cancel(true);
        }
    }

    private class LoadImageTask extends AsyncTask<Void,Void,Bitmap>{

        public String mUrl;
        public LoadImageTask(String url){
             mUrl = url;
        }

        @Override
        protected Bitmap doInBackground(Void... voids) {
            //返回线程结果
            return HttpUtil.loadImage(mUrl);

        }

        @Override
        protected void onPostExecute(Bitmap s) {
            super.onPostExecute(s);
            //传递结果更新UI界面
            if (s != null){
                mImageView.setImageBitmap(s);
            }
        }
    }
}
